# -*- coding:utf-8 -*-
from pre_processing import *
import matplotlib.pyplot as plt

class Data(object):
    links=Links()
    routes=Routes()
    trajectories=Trajectories()
    volumes=Volumes()
    weathers=Weathers()

    def visualizeByDay(self, *cols):
        '''
            输入多个需要visualize的变量名，可选的有:
                'weathers.get(dates, {property},{seg1},{seg2}) ...
                'volumes.getByHour(dates, {toll},{dire},{seg1},{seg2})' , seg belongs to 0-71
                'trajectories.averageTime(dates, {linkid}, {seg1}, {seg2})'
                'trajectories.averageTravelTime(dates, {intersection_id}, {tollgate_id}, {seg1}, {seg2})'
        '''
        colors = ['red', 'blue', 'green', 'yellow', 'black', 'cyan']
        dates = None
        for i in range(len(cols)):
            if cols[i][0] == 'w':
                tmp = self.weathers.available_dates()
            if cols[i][0] == 'v':
                tmp = self.volumes.available_dates()
            if cols[i][0] == 't':
                tmp = self.trajectories.available_dates()
            if dates is None:
                dates = set(tmp)
            dates = dates & set(tmp)
        x = sorted(list(dates))
        print('visualize %d days' % len(x))
        plt.figure(figsize = (20, 10))
        for i in range(len(cols)):
            y = eval('self.' + cols[i])
            if cols[i][0] == 'w':
                pass
            if cols[i][0] == 'v':
                y = [len(cars) for cars in y]
            if cols[i][0] == 't':
                pass
            print y
            print('%d days of ' % len(y) + cols[i])
            meany = sum(y) * 1.0 / len(y)
            y = [v - meany for v in y]
            plt.plot(x, y,label = cols[i], color = colors[i], linewidth = 1)
        plt.legend()
        plt.show()

    def __init__(self):
        readLinks(self.links)
        readRoutes(self.routes)
        readTrajectories(self.trajectories)
        readVolumes(self.volumes)
        readWeathers(self.weathers)

if __name__ == '__main__':
    data=Data()
    # res1=data.weathers.available_dates()
    # res2=data.trajectories.available_dates()
    # res3=data.volumes.available_dates()
    # print len(res1),len(res2),len(res3)
    # res=data.volumes.get(1,0,datetime.date(2016,9,19))
    # for i in res[1]:
    #     i.print_info()
    data.visualizeByDay("weathers.get(dates, 'pressure',16,19)", "weathers.get(dates, 'temperature',16,19)",'volumes.getByHour(dates, 2,0,16,19)',
        'trajectories.averageTravelTime(dates, "A", 2, 16, 19)')
